home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / AmigaSystem / Scalos / PreferencesLib / examples / C / single.c < prev    next >
C/C++ Source or Header  |  2002-10-28  |  3KB  |  88 lines

  1. /*  Simple example of using single data items per tag. */
  2.  
  3. /* Always included first */
  4. #include <exec/types.h>
  5.  
  6. /* So we can use the functions from these libraries */
  7. #include <proto/dos.h>
  8. #include <proto/exec.h>
  9. #include <proto/preferences.h>
  10.  
  11. /* For the MAKE_ID macro */
  12. #include <libraries/iffparse.h>
  13.  
  14. /* For the PrefsStruct structure */
  15. #include <scalos/preferences.h>
  16.  
  17. /* some standard stuff */
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21.  
  22. /* Pointer to the preferences.library base */
  23. struct Library  *PreferencesBase;
  24.  
  25.  
  26. int main(void)
  27. {
  28.     STRPTR  prefname = "Example";       /* Name to refer to our PrefsHandle with */
  29.     APTR    prefhandle;                 /* Handle to our preferences set */
  30.     ULONG   ID, Tag;                    /* ID and Tag are used to access a specific preferences item */
  31.     STRPTR  prefdata = "This is some example preference data";
  32.     struct PrefsStruct  *prefstruct;    /* Used to find the data in the PrefsHandle */
  33.     char    temp[128];                  /* Space to retrieve the data from the PrefsHandle */
  34.     LONG    datasize;                   /* Size of data retrieved from PrefsHandle */
  35.  
  36.  
  37.     if(NULL != (PreferencesBase = OpenLibrary("preferences.library", 39)) )
  38.     {
  39.         /* First we need to allocate a PrefsHandle by name */
  40.         if(NULL != (prefhandle = AllocPrefsHandle(prefname)) )
  41.         {
  42.             printf("PrefsHandle successfully allocated\n");
  43.  
  44.             /* We need an ID and Tag value to refer to a specific preference.
  45.              * so we create that here (you could always just put them in the library calls).
  46.              * Remember that the ID must be created from 4 ASCII characters and the Tag
  47.              * cannot be 0
  48.              */
  49.             ID = MAKE_ID('M','A','I','N');
  50.             Tag = 1;
  51.  
  52.             /* Store the preference data in the PrefsHandle, under the ID and Tag.
  53.              * The reason we pass a length of strlen()+1 is so that the NULL terminator
  54.              * for the string will definately be stored as well
  55.              */
  56.             SetPreferences(prefhandle, ID, Tag, prefdata, strlen(prefdata)+1);
  57.  
  58.             /* Use FindPreferences to check that the item has been stored */
  59.             if(NULL != (prefstruct = FindPreferences(prefhandle, ID, Tag)) )
  60.             {
  61.                 printf("Preference data found\n");
  62.                 printf("Size of data stored:   %lu\n", strlen(prefdata)+1);
  63.                 printf("Size of preference data: %d\n", prefstruct->ps_Size);
  64.  
  65.                 /* And to show that the data was actually stored, we get it back
  66.                  * using GetPreferences
  67.                  */
  68.                 if(0 < (datasize = GetPreferences(prefhandle, ID, Tag, temp, 128)) )
  69.                 {
  70.                     printf("Bytes retrieved from PrefsHandle: %ld\n", datasize);
  71.                     printf("Contents of preference data:\n    %s\n", temp);
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 printf("Preference data was not stored :(\n");
  77.             }
  78.  
  79.             /* Finally, we free the PrefsHandle */
  80.             FreePrefsHandle(prefhandle);
  81.         }
  82.  
  83.         CloseLibrary(PreferencesBase);
  84.     }
  85.     return(0);
  86. }
  87.  
  88.